"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".

Unload the initial_data.json fixture before running tests.
"""

from django.test import TestCase
from datetime import time
from models import *

class SimpleTest(TestCase):
    def test_basic_addition(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        self.failUnlessEqual(1 + 1, 2)

class PreferenceTest(TestCase):

    def setUp(self):
        self.p = PreferencesFilter(earliest_class_start=time(minute=45, hour=8), latest_class_end=time(minute=45,hour=20))
	
    def test_pref(self):
        self.failUnlessEqual(self.p.earliest_class_start.minute, 45)
        self.failUnlessEqual(self.p.earliest_class_start.hour, 8)

class CourseSlotTest(TestCase):
  
    fixtures = ['courseslot_test.json']

    def setUp(self):
        self.course_slot1 = CourseSlot.objects.get(pk=1)
        self.course_slot2 = CourseSlot.objects.get(pk=2)
        self.course_slot3 = CourseSlot.objects.get(pk=3)
        self.course_slot4 = CourseSlot.objects.get(pk=4)
        self.course_slot5 = CourseSlot.objects.get(pk=5)
        self.section1 = Section.objects.get(pk = 6)
        self.semester1 = Section.objects.get(pk = 6).semester
        self.course1 = Course.objects.get(pk = 8)
        
    def test_conflicts_with(self):
        # Tests some conflicts situations
        self.failUnlessEqual(self.course_slot1.conflicts_with(self.course_slot2), False)
        self.failUnlessEqual(self.course_slot1.conflicts_with(self.course_slot3), True)
        self.failUnlessEqual(self.course_slot1.conflicts_with(self.course_slot4), True)
        self.failUnlessEqual(self.course_slot1.conflicts_with(self.course_slot5), False)

        # Test the other way around
        self.failUnlessEqual(self.course_slot2.conflicts_with(self.course_slot1), False)
        self.failUnlessEqual(self.course_slot3.conflicts_with(self.course_slot1), True)
        self.failUnlessEqual(self.course_slot4.conflicts_with(self.course_slot1), True)
        self.failUnlessEqual(self.course_slot5.conflicts_with(self.course_slot1), False)
         
    def test_getSection(self):
        #this only seems to works when i delete the initial data file
        self.failUnlessEqual(self.course_slot1.get_section(), self.section1)
        
    def test_getCourse(self):
        #same thing over here
        self.failUnlessEqual(self.course_slot1.get_course(), self.course1)
        
    def test_getSemester(self):
        #and here
        self.failUnlessEqual(self.course_slot1.get_semester(), self.semester1)
class SectionTest(TestCase):

    fixtures = ['section_test.json']

    def setUp(self):
        self.section1 = Section.objects.get(pk=6)
        self.section2 = Section.objects.get(pk=7)
	self.course1 = Course.objects.get(pk=8)

    def test_conflicts_with(self):
        self.failUnlessEqual(self.section1.conflicts_with(self.section2), True)
        self.failUnlessEqual(self.section2.conflicts_with(self.section1), True)
    
    def test_str(self):
        self.assertEqual(self.section1.__str__(), u'Computers for dummies Section code: XA')
        self.assertEqual(self.section2.__str__(), u'Computers for dummies Section code: TL')
        
        
    def test_get_course(self):
        self.failUnlessEqual(self.course1.__str__(), u'SOEN 101 Computers for dummies')
        self.assertEqual(self.section1.get_course(), self.course1)
        self.failUnlessEqual(self.section2.get_course(), self.course1)
        
class CourseTest(TestCase):
    
    fixtures = ['course_test.json']
  
    def setUp(self):
        self.course1 = Course.objects.get(pk=10)
        
    def test_str(self):
        self.failUnlessEqual(self.course1.__str__(), u'SOEN 101 Computers for dummies')
        self.failUnlessEqual(self.course1.sections.all()[0].get_course(), self.course1)
        self.failUnlessEqual(self.course1.sections.all()[1].get_course(), self.course1)
        

class ScheduleTest(TestCase):
  
    fixtures = ['schedule_test.json']

    def setUp(self):
      
        self.cslot1 = CourseSlot.objects.get(pk=1)
        self.cslot2 = CourseSlot.objects.get(pk=2)
        self.cslot3 = CourseSlot.objects.get(pk=3)
        self.cslot4 = CourseSlot.objects.get(pk=4)
        self.cslot5 = CourseSlot.objects.get(pk=5)
        self.cslot6 = CourseSlot.objects.get(pk=6)
        self.cslot7 = CourseSlot.objects.get(pk=7)
        
        self.sched1 = Schedule.objects.get(pk=12)
        self.section1 = Section.objects.get(pk=8)
        self.section2 = Section.objects.get(pk=9)
        
    def test_addAndRemoveSection(self):

        #removing section which is not there
        self.failUnlessEqual(self.sched1.remove_section(self.section1), None)
        
        # adding sections
        self.assertEqual(self.sched1.add_section(self.section1), True)
        self.assertEqual(self.sched1.add_section(self.section2), True)
        #adding section which is already there
        self.failUnlessEqual(self.sched1.add_section(self.section2), False)
        self.sched1.save()
        
        self.assertQuerysetEqual(self.sched1.sections.all(), ['<Section: Intro to talking Section code: XA>', 
							      '<Section: Speaking Section code: AA>'])
        
        # removing sections
        self.sched1.remove_section(self.section2)
        self.sched1.remove_section(self.section1)
        self.sched1.save()
        
        self.assertQuerysetEqual(self.sched1.sections.all(), [])


        
    def test_getAllCoursesSlots(self):
        self.setUp()

        # adding sections
        self.sched1.add_section(self.section1)
        self.sched1.add_section(self.section2)
        self.sched1.save()
        self.assertQuerysetEqual(self.sched1.sections.all(), ['<Section: Intro to talking Section code: XA>', 
							      '<Section: Speaking Section code: AA>'])
          
	sched1_cslots = self.sched1.get_all_course_slots()
	self.failUnlessEqual(sched1_cslots[0].__str__(), u'M (08:45 - 11:30)')
        
    def test_getOutputData(self):
        
        # adding sections
        self.sched1.add_section(self.section1)
        self.sched1.add_section(self.section2)
        self.sched1.save()
        
        expected_output = [
		[self.cslot1, self.cslot2, self.cslot3],
		[self.cslot4, self.cslot6],
		[],
	        [self.cslot5, self.cslot7],
	        []
	  ]
        self.failUnlessEqual(self.sched1.get_output_data(), expected_output) 


    def test_str(self):
        self.sched1.add_section(self.section1)
        self.sched1.add_section(self.section2)
        self.sched1.save()
        self.failUnlessEqual(self.sched1.__str__(), 'Schedule #12 \n<br/>\nIntro to talking Section code: XA\n<br/>\nSpeaking Section code: AA\n<br/>\n')

class MetaScheduleTest(TestCase):
    fixtures = ['metaschedule_test.json']
    
    def setUp(self):
        self.metasched1 = MetaSchedule.objects.get(pk=14)

    def test_generate(self):
        i = 0
        while i < 10:
            iternext = self.metasched1.generate(i, 'W')
            self.metasched1.save()
            print iternext
            if iternext < i:
                break
            i = iternext + 1
            print self.metasched1.schedules.all()
